package org.skydiveapphd;

import org.skydiveapphd.interfaces.DiagramNotifier;
import org.skydiveapplibrary.R;
import org.skydiveapplibrary.ui.DrawingView;

import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class LogEntryDiagramActivity extends Fragment
{
	// listeners
	private OnClickListener doneListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			save();
		}
	};	
	private OnClickListener clearListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			clear();
		}
	};
	private OnClickListener cancelListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			getFragmentManager().popBackStack();
		}
	};
	
	public static LogEntryDiagramActivity newInstance(Bundle args) 
	{
		LogEntryDiagramActivity f = new LogEntryDiagramActivity();
		f.setArguments(args);
		return f;
	}
	
	
	@Override 
    public void onCreate(Bundle savedInstanceState)
    { 
    	super.onCreate(savedInstanceState);
    }
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    		Bundle savedInstanceState)
	{	
	    // Inflate the layout for this fragment
	    return inflater.inflate(R.layout.log_entry_diagram_view, container, false);
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		
    	// init header
    	TextView headerText = (TextView) getActivity().findViewById(R.id.FormHeaderTextView);
    	headerText.setText(R.string.log_entry_diagram_header);
    	
    	// add button listeners
    	ImageButton doneButton = (ImageButton) getActivity().findViewById(R.id.DiagramSaveButton);
    	doneButton.setOnClickListener(doneListener);
    	
    	ImageButton clearButton = (ImageButton) getActivity().findViewById(R.id.DiagramClearButton);
    	clearButton.setOnClickListener(clearListener);
    	
    	ImageButton cancelButton = (ImageButton) getActivity().findViewById(R.id.DiagramCancelButton);
    	cancelButton.setOnClickListener(cancelListener);
    	
    	// get drawingview 
    	DrawingView drawingView = (DrawingView) getActivity().findViewById(R.id.LogEntryDiagramDrawingView);
    	// load data
    	byte[] data = getArguments().getByteArray(Constants.EXTRA_DIAGRAM);
    	if (data != null)
    	{
    		Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    		drawingView.drawBitmap(bitmap);
    	}
	}
	
	private void clear()
	{
		DrawingView drawingView = (DrawingView) getActivity().findViewById(R.id.LogEntryDiagramDrawingView);
		drawingView.clear();
	}
	
	private void save()
	{
		DrawingView drawingView = (DrawingView) getActivity().findViewById(R.id.LogEntryDiagramDrawingView);

		if (drawingView.isBitmapChanged())
		{
			// get image data
			byte[] data = drawingView.getImageData();
			
			DiagramNotifier f = (DiagramNotifier) getFragmentManager().findFragmentByTag(getArguments().getString(Constants.EXTRA_FRAG_TAG));
			f.updateDiagram(data);
		}
		
		// finish
		getFragmentManager().popBackStack();
	}
}
